package com.moon.game.command.guest.market;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import org.apache.log4j.Logger;

import com.moon.game.command.BaseCmd;
import com.moon.game.command.RequestSession;
import com.moon.game.constants.Constants;
import com.moon.game.constants.ErrorCode;
import com.moon.game.exception.GException;
import com.moon.game.manager.ItemMG;
import com.moon.game.resource.model.Item;
import com.moon.game.service.MainLandService;
import com.moon.game.util.Utils;
import com.moon.game.vos.ErrorMsg;


/**
 * @author Ranger 作者 E-mail:haozi000005@163.com
 * @version 创建时间：2012-4-26 下午4:32:04
 * @description 商城
 */
public class Market extends BaseCmd {

	private static final Logger log = Logger.getLogger(Market.class);
	@Override
	protected void runOnce(RequestSession req) throws GException {
		switch (req.getStatus()) {
			case STATU_INIT:
				doInit(req);
				break;
			case STATU_CHECK:
				doCheckSelf(req);
				break;
			case STATU_RETURN:
				doResponse(req);
				break;
			default:
				break;
		}
	}

	@Override
	protected void doResponse(RequestSession req) throws GException {
		String[] ps = req.getClientRequest().getPs();
		if (ps == null || ps.length < 2) {
			doResponsePsError(req);
			return;
		}
		Integer type = Integer.parseInt(ps[0]);
		Integer page = Integer.parseInt(ps[1]);
		if (type == null) {
			doResponseErrorMsg(req,ErrorMsg.build(ErrorCode.ERR_PAR, "商城类型错误！"));
			return;
		}
		
		ArrayList<Integer> resGoods = new ArrayList<Integer>();
		StringBuffer info = new StringBuffer();
		if (type.intValue() == Constants.MONEY_ITEM_TYPE) {
			int location = getPlayerVo(req).getLocation();
			if (MainLandService.getMainLand(location).getLevel() != 0) {
				//人物没有在主城
				log.error(req.getPlayerId() + "location is" + location + ",buy money item fail!!");
				doResponseErrorMsg(req,ErrorMsg.build(ErrorCode.ERR_LOCATION, "您所在的位置不能购买碎银道具！"));
				return;
			}
			info.append("欢迎光临碎银摊位，在这里你可以买到下面的东西~");
			//碎银商城
			resGoods = ItemMG.instance.getMoneyGoods();
		}else if (type.intValue() == Constants.GOLD_ITEM_TYPE) {
			info.append("欢迎光临金币商城，在这里你可以买到下面的东西~");
			//金币商城
			resGoods = ItemMG.instance.getGoldGoods();
		}
		
		req.setVO(TAG_INFO, info.toString());
		//总页数
		if (resGoods != null && resGoods.size() > 0) {
			req.setVO("total_page", Utils.totalPage(resGoods.size(), Constants.PAGE_SIZE));
		}
		
		//将其他道具放装备道具前面
		LinkedList<Integer> firstOtherGoods = new LinkedList<Integer>();
		for (Integer goodsId : resGoods) {
			final Item item = ItemMG.instance.getItem(goodsId);
			if (item != null) {
				if (item.getPosition() == 0) {
					firstOtherGoods.addFirst(goodsId);
				}else {
					firstOtherGoods.addLast(goodsId);
				}
			}
		}
		
		//将适合玩家等级的道具放前面【正负3级的放前面其余的放后面】
		LinkedList<Integer> fixPlayerGoods = new LinkedList<Integer>();
		for (Integer goodsId : firstOtherGoods) {
			final Item item = ItemMG.instance.getItem(goodsId);
			if (item != null) {
				int intervalLevel = req.getPlayer().getLevel() - item.getLevel();
				if (intervalLevel < 3 && intervalLevel > -3) {
					fixPlayerGoods.addFirst(goodsId);
				}else {
					fixPlayerGoods.addLast(goodsId);
				}
			}
		}
		
		//分页处理
		List<Object> pageGoods = Utils.page(fixPlayerGoods, page.intValue(), Constants.PAGE_SIZE);
		ArrayList<Integer> retGoods = new ArrayList<Integer>();
		for (Object obj : pageGoods) {
			Integer id = (Integer)obj;
			if (id == null || id.intValue() == 0) {
				continue;
			}
			retGoods.add(id);
		}
		req.setVO("goods", retGoods);
		req.setVO("money", req.getPlayer().getMoney());
		req.setVO("gold", req.getPlayer().getGold());
		req.setVO("curr_type", type);
		req.setVO("curr_page", page);
		response(req, req.getClientResponse());
	}

}
